const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require("cors");
require('dotenv').config();
const adminAuth = require("./middlewares/admin-auth")
const userAuth = require("./middlewares/user_auth")
// const uploadsRouter = require('/routes/uploads')

const indexRouter = require('./routes/index');
const categoriesRouter = require('./routes/categories');
const coursesRouter = require('./routes/courses');
const chaptersRouter = require('./routes/chapters');
const articleRouter = require('./routes/articles');
const searchRouter = require('./routes/search');
const authRouter = require("./routes/auth")
const userRouter = require("./routes/users")
const likeRouter = require("./routes/likes")
const captchaRouter = require("./routes/captcha")
// 后台路由文件
const adminArticlesRouter = require("./routes/admin/articles")
const adminCategoriesRouter = require("./routes/admin/categories")
const adminSettingsRouter = require("./routes/admin/settings")
const adminUsersRouter = require("./routes/admin/users")
const adminCoursesRouter = require("./routes/admin/courses")
const adminChaptersRouter = require("./routes/admin/chapters")
const adminChartsRouter = require("./routes/admin/charts")
const adminAuthRouter = require("./routes/admin/auth")
const adminAttachmentRouter = require("./routes/admin/attachments")

const app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors())

app.use("/admin/articles",adminAuth,adminArticlesRouter)
app.use("/admin/categories",adminAuth,adminCategoriesRouter)
app.use("/admin/settings",adminAuth,adminSettingsRouter)
app.use("/admin/users",adminAuth,adminUsersRouter)
app.use("/admin/courses",adminAuth,adminCoursesRouter)
app.use("/admin/chapters",adminAuth,adminChaptersRouter)
app.use("/admin/charts",adminAuth,adminChartsRouter)
app.use("/admin/attachment",adminAuth,adminAttachmentRouter)
app.use("/admin/auth",adminAuthRouter)

// 前台路由
app.use("/categories",userAuth,categoriesRouter);
app.use("/courses",userAuth,coursesRouter)
app.use("/chapters",userAuth,chaptersRouter)
app.use("/article",userAuth,articleRouter)
app.use("/search",userAuth,searchRouter)
app.use("/users",userAuth,userRouter)
app.use("/likes",userAuth,likeRouter)
app.use("/auth",authRouter)
app.use("/captchaRouter",captchaRouter)
app.use('/',userAuth,indexRouter);

// app.use('/uploads',userAuth,uploadsRouter)

module.exports = app;
